﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Control;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Repository;

namespace LPV_POS.mh_pos
{
    public partial class mh_baocaodoanhthu_nhomthe : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        DataTable tb;
        DataSet ds;

        public mh_baocaodoanhthu_nhomthe()
        {
            InitializeComponent();
            InitControl();
            InitGrid();
            LoadDataGrid();
        }

        #region 1.InitControl

        private void InitControl()
        {
            f_showdataoncombox(CboKhachHang, f_selectdsNhomTheforcom(), "OBJID", "OBJNAME");
            CboKhachHang._SetselectedID("-1");

        }

        // hien thi ds nhom chu len combobox
        private void f_showdataoncombox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }

        #endregion

        #region 2.Khởi Tạo Lưới

        private void InitGrid()
        {
            Col_Nhomthe.FieldName = "OBJNAME";
            Col_GiamGia.FieldName = "GIAM_GIA";
            CotCal(Col_TongTienCan,"TONG_TIEN_KM");
            CotCal(Col_TongTienDatDuoc, "TONGTIEN_CON");
            Col_GhiChu.FieldName = "GHI_CHU";
        }

        private void LoadDataGrid()
        {
            ds = GetData(Convert.ToInt32(CboKhachHang._GetselectedID()));
            try
            {
                GridControl_Data.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception)
            {
                
            }
        }

        #endregion

        #region 3.Database

        // select danh sach Khách hàng
        private DataTable f_selectdsNhomTheforcom()
        {
            tv = new thuvien();
            tb = tv.sql_SelectDataset("DM_THE_NHOM").Tables[0];
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "-1";
            myNewRow["OBJNAME"] = "--- Chọn nhóm thẻ ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        private DataSet GetData(int NHOMTHE_MA)
        {
            string[] s;
            object[] o;
            s = new string[] { "@NHOMTHE_MA"};
            o = new object[] { NHOMTHE_MA };
            DataSet ds = tv.SP_SelectDataset("sp_DM_THE_NHOM_Select", s, o);
            return ds;
        }

        #endregion

        #region 4.Hỗ trợ

        public static void CotCal(GridColumn Column, string FieldName)
        {
            RepositoryItemCalcEdit Cal = new RepositoryItemCalcEdit();
            ((System.ComponentModel.ISupportInitialize)(Cal)).BeginInit();

            Cal.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});

            Column.ColumnEdit = Cal;
            Column.FieldName = FieldName;
            Cal.Name = "Cal";
            Cal.DisplayFormat.FormatString = "N0";
            Cal.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            ((System.ComponentModel.ISupportInitialize)(Cal)).EndInit();
        }

        #endregion

        #region Xử lý nút

        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int i = tv.SP_inser_alltable("BH_PHIEU_NGUOI_GIOI_THIEU", ds.Tables[0]);
            if (i > 0)
            {
                MessageBox_LPV.Show("Lưu dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox_LPV.Show("Lưu dữ liệu không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkAll.Checked == true)
            {
                CboKhachHang._SetselectedID("-1");
                CboKhachHang.Properties.ReadOnly = true;
            }
            else
                CboKhachHang.Properties.ReadOnly = false;
        }

        private void BtnExportExcel_Click(object sender, EventArgs e)
        {
            cpublic c = new cpublic();
            c.ATExportExcel(GridView_Data);
        }

        #endregion
    }
}